import 'package:flutter/material.dart';
import 'package:collins_vocabulary/components/category.dart';
import 'package:collins_vocabulary/components/wordcard.dart';
import 'package:collins_vocabulary/components/setting.dart';
import 'package:collins_vocabulary/components/dictionary.dart';
import 'package:flutter/services.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:titled_navigation_bar/titled_navigation_bar.dart';
import 'dart:async';

void main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: '柯林斯核心',
      theme: ThemeData(
        appBarTheme: AppBarTheme(brightness: Brightness.light),
        primaryIconTheme: const IconThemeData(color: Colors.black),
        fontFamily: 'yuyang',
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  Widget tabView;
  SharedPreferences prefs;
  PageController pageController;
  int currentIndex = 0;

  Future<SharedPreferences> initPrefs() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final level = prefs.getInt('level');
    if (level == null) {
      prefs.setInt('level', 5);
      prefs.setInt('count', 50);
      prefs.setBool('showcollins', true);
      prefs.setBool('sentence', true);
      prefs.setBool('showCn', true);
      prefs.setBool('autoplay', true);
      prefs.setBool('en_ph', true);
    }
    prefs.remove('studied');
    final studied = prefs.getString('studied');
    if (studied == null) {
      prefs.setString('studied', '');
      prefs.setInt('studying', 0);
    }
    return prefs;
  }

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: currentIndex, keepPage: true);
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }

  void _onTap(int index) {
    if ((currentIndex - index).abs() == 1) {
      pageController.animateToPage(index,
          duration: const Duration(milliseconds: 300), curve: Curves.ease);
    } else {
      pageController.jumpToPage(index);
    }
  }

  pageChanged(int page) {
    setState(() {
      currentIndex = page;
    });
  }

  final _drawerController = ZoomDrawerController();

  bool _showDraw = false;
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      builder: (BuildContext context, snapshot) {
        if (snapshot.hasData) {
          return ZoomDrawer(
            controller: _drawerController,
            style: DrawerStyle.Style1,
            menuScreen: Mine(prefs: snapshot.data),
            mainScreen: Scaffold(
              appBar: AppBar(
                elevation: 0,
                title: Text(
                  '柯林斯核心',
                  style: TextStyle(
                    color: Color(0xFF7F8AEE),
                  ),
                ),
                backgroundColor: Colors.white,
                leading: Builder(
                  builder: (BuildContext context) {
                    return IconButton(
                      icon: Icon(
                        Icons.menu,
                        color: Color(0xFF7F8AEE),
                      ),
                      onPressed: () {
                        setState(() {
                          ZoomDrawer.of(context).toggle();
                        });
                      },
                    );
                  },
                ),
              ),
              body: PageView(
                controller: pageController,
                onPageChanged: pageChanged,
                children: <Widget>[
                  RememberVocab(prefs: snapshot.data),
                  WordList(prefs: snapshot.data),
                  // Dictionary(),
                ],
              ),
              bottomNavigationBar: TitledBottomNavigationBar(
                currentIndex: currentIndex,
                onTap: _onTap,
                activeColor: Color(0xFF7F8AEE),
                items: [
                  TitledNavigationBarItem(
                    title: Text('背单词'),
                    icon: Icon(
                      Icons.lightbulb,
                      size: 24.0,
                    ),
                  ),
                  TitledNavigationBarItem(
                    title: Text('分类'),
                    icon: Icon(
                      Icons.library_books,
                      size: 24.0,
                    ),
                  ),
                  // TitledNavigationBarItem(
                  //   title: Text('词典'),
                  //   icon: Icon(
                  //     Icons.translate,
                  //     size: 24.0,
                  //   ),
                  // ),
                ],
              ),
            ),
            borderRadius: 26,
            showShadow: true,
            angle: -7,
            backgroundColor: Colors.grey[300],
            slideWidth: MediaQuery.of(context).size.width * .8,
            openCurve: Curves.fastOutSlowIn,
            closeCurve: Curves.fastOutSlowIn,
          );
        }
        return Center(
          child: CircularProgressIndicator(),
        );
      },
      future: initPrefs(),
    );
  }
}
